//
//  PurchaseTableViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/7/12.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

enum FilePath: String {
    
    case Purchase = "purchasingManage/pc/sys/attachFile/download"//采购
    
    case Project = "projectManage/pc/sys/attachFile/download"//项目
    
    case Contract = "marketingManage/pc/sys/attachFile/download"//合同查看附件接口
    
    case Finacial = "financesAccesory/pc/sys/attachFile/download"//差旅报销
//    /mobile/sys/attachFile/download
    
}

class PurchaseTableViewController: TitleContentTableViewController {
    
    var filePath: FilePath = FilePath.Purchase
    
    var _documents:Array<JSON> = []
    
    var documents: Array<JSON>{
        
        set{
            //config footer
            _documents = newValue
        
            self.tableView.reloadData()
        }
        
        get{
            return _documents
        }
    }
    
    @objc func showDoc(id: String) -> Void {
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return super.tableView(tableView, numberOfRowsInSection: section)
        default:
            return self.documents.count
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return super.tableView(tableView, heightForRowAt: indexPath)
        } else {
            let name = documents[indexPath.row].contents?["name"]?.rawString() ?? ""
            if let cell = tableView.cellForRow(at: indexPath) {
                let size = name.size(with: cell.textLabel!.font, maxSize: CGSize.init(width: self.view.width - 48, height: 10000))
                return size.height > 55 ? size.height  : 55
            }
        }
        return 55
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch indexPath.section {
            
        case 1:
            
            var cell = tableView.dequeueReusableCell(withIdentifier: "Attachment")
            
            if cell == nil {
                cell = UITableViewCell.init(style: .default, reuseIdentifier: "Attachment")
                cell?.textLabel?.numberOfLines = 0
            }
            
            cell?.imageView?.image = UIImage(named: "icon_attachment")
            
            cell?.textLabel?.font = UIFont.systemFont(ofSize: 15)
            
            cell?.textLabel?.textColor = UIColor.hex("458AE0")
            
            if indexPath.row < documents.count {
                cell?.textLabel?.text = documents[indexPath.row].contents?["name"]?.rawString()
            }
            
            return cell!
            
        default:
            return super.tableView(tableView, cellForRowAt: indexPath)
        }
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        switch indexPath.section {
        case 0:
            return
        default:
            
            let load = loadExcelViewController()
            if let id = documents[indexPath.row].contents?["id"]?.rawString() {
                let p = Server + filePath.rawValue + "?id=" + id
                NetManager.download(path: p, prog: { (progress) in
                    SVProgressHUD.showProgress(Float(progress))
                }) { (file) in
                    
                    SVProgressHUD.dismiss()
                    
                    let previewURL = PreviewURL(url: file, name: self.documents[indexPath.row].contents?["name"]?.rawString())
                    
                    load.path = previewURL
                    
                    let root = UIApplication.shared.keyWindow?.rootViewController
                    root?.present(load, animated: true, completion: nil)
                    
                }
            }
            
            return
        }
    }
    
    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 0:
            return 10
        default:
            return 40
        }
    }
    
    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 1:
            let footer = UIView()
            
            footer.backgroundColor = UIColor.dftBack
            
            footer.backgroundColor = UIColor.white
            
            let titleLbl = UILabel()
            
            titleLbl.textColor = UIColor.textColor
            
            titleLbl.font = UIFont.systemFont(ofSize: 15)
            
            titleLbl.text = "附件:"
            
            titleLbl.frame = CGRect(origin: CGPoint(x: 15.0, y: 15.0), size: CGSize(width: titleLbl.contentSize.width, height: titleLbl.contentSize.height))
            
            footer.addSubview(titleLbl)
            
            return footer
        default:
            return nil
        }
    }

}
